﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GIC.BO.Access;
using GIC.DAO.Model;
using GIC.UI.Helper;
using System.Threading;

namespace GIC.UI
{
    public partial class ReEnvio : Form
    {
        Form form;
        GICBo bo = new GICBo();
        private Parametros parametrosEntity = new Parametros();

        public ReEnvio()
        {
            InitializeComponent();
            this.TopMost = true;
        }

        public ReEnvio(Form form)
        {
            InitializeComponent();
            this.form = form;
            this.TopMost = true;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            this.form.Enabled = true;
        }

        private void buttonStartListen_Click(object sender, EventArgs e)
        {
            if (this.txtBodyNo != null && this.txtBodyNo.Text != "")
            {
                if (this.txtBodyNo.Text.Length < 10)
                {
                    MessageBox.Show("O Body No necessita ter pelo menos 10 caracteres",
                                         "Body No inválido",
                                         MessageBoxButtons.OK,
                                         MessageBoxIcon.Error,
                                         MessageBoxDefaultButton.Button1);
                }
                else
                {
                    BodyNoReenvio bodyNoReenvio = new BodyNoReenvio();

                    bodyNoReenvio.BodyNo = this.txtBodyNo.Text;
                    bodyNoReenvio.Status = Enum.GetName(typeof(StatusBodyNo), StatusBodyNo.Pendente);
                    bodyNoReenvio.Data = DateTime.Now;

                    bo.salvar(bodyNoReenvio);

                    this.Close();
                    this.form.Enabled = true;

                    parametrosEntity = bo.obterParametro();

                    if (parametrosEntity == null)
                    {
                        parametrosEntity = new Parametros();
                        parametrosEntity.socketPorta = 9016;
                        parametrosEntity.socketIP = "127.0.0.1";
                        bo.salvar(parametrosEntity);
                    }
                }
            }
            else
            {
                MessageBox.Show("Body No inválido",
                                          "",
                                          MessageBoxButtons.OK,
                                          MessageBoxIcon.Error,
                                          MessageBoxDefaultButton.Button1);
            }
        }
    }



}
